package com.rinvay.asdownloader.ui;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;


import com.rinvay.asdownloader.R;
import com.rinvay.network.access.NetworkAccess;
import com.rinvay.network.http.HttpRequest;
import com.rinvay.network.http.conn.impl.DefaultHttpConnection;
import com.rinvay.network.http.conn.impl.DefaultHttpConnectionListener;
import com.rinvay.network.http.conn.impl.HttpConnectionManager;
import com.rinvay.network.http.exception.ErrorMessage;
import com.rinvay.utils.logger.Logger;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class TestActivity extends Activity
{
    private static final String TAG = "TestActivity";
    
    HttpConnectionManager httpmgr = new HttpConnectionManager();

    protected void onCreate(Bundle savedInstanceState) {
        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maintest);
        
        File file = new File(
                android.os.Environment.getExternalStorageDirectory(),
                "log_mcloud.txt");
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file, true);
            Logger.info(TAG, "new file");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        
        Button testButton = (Button) findViewById(R.id.test);
        testButton.setOnClickListener(new OnClickListener() {
            
            public void onClick(View v) {
//                String s = null;
//                System.out.println(s.equals(""));
//                startScan(REQUEST_SCAN_FOR_ADD);
//                NetworkAccess instance = NetworkAccess.getInstance();
//                instance.setNetWorkValid(!instance.isNetWorkValid());
//                TextView text1 = (TextView) findViewById(R.id.textView1);
//                text1.setText(instance.isNetWorkValid() + "");
//                
//                testhttp();
            }
        });
    }
    
    private void testhttp() {
        String strurl = "http://www.baidu.com";
        HttpRequest request = new HttpRequest(strurl);
        DefaultHttpConnection conn = (DefaultHttpConnection) httpmgr.createDefaultHttpConnection(request, new DefaultHttpConnectionListener() {
            
            public void onError(int errorcode, String errorMsg) {
                Logger.info(TAG, errorcode +  " " + ErrorMessage.getErrorMessage(errorcode));
            }
            
            public void onConnecting() {
            }
            
            public void onConnected() {
            }
            
            public void onResponse(byte[] resp) {
                Logger.info(TAG, new String(resp));
            }
        });
        httpmgr.startConnection(conn);
    }

    
    private void startScan(int code) {
        try {
            final Intent intent = new Intent(ScanIntent.INTENT_ACTION_SCAN);
            intent.putExtra(ScanIntent.INTENT_EXTRA_SCAN_MODE,
                    ScanIntent.INTENT_EXTRA_PRODUCT_MODE);
            startActivityForResult(intent, code);
        } catch (ActivityNotFoundException e) {
//            UIUtilities.showToast(this, R.string.error_missing_barcode_scanner, true);
        }
    }
    
    public final class ScanIntent {
        public static final String INTENT_ACTION_SCAN = "com.google.zxing.client.android.SCAN";
        public static final String INTENT_EXTRA_SCAN_MODE = "SCAN_MODE";
        public static final String INTENT_EXTRA_PRODUCT_MODE = "PRODUCT_MODE";
        public static final String SCAN_RESULT = "SCAN_RESULT";
        public static final String SCAN_RESULT_FORMAT = "SCAN_RESULT_FORMAT";
        public static final String FORMAT_EAN_13 = "EAN_13";

        ScanIntent() {
        }

//        public static boolean isInstalled(Context context) {
//            final PackageManager packageManager = context.getPackageManager();
//            final Intent intent = new Intent(INTENT_ACTION_SCAN);
//            List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
//                    PackageManager.MATCH_DEFAULT_ONLY);
//            return list.size() > 0;
//        }
    }
    private static final int REQUEST_SCAN_FOR_ADD = 1;
    private static final int REQUEST_SCAN_FOR_CHECK = 2;
}
